# -*- Python -*-

# Configuration file for the 'lit' test runner.

import os
import platform
import subprocess

import lit.formats
import lit.util

# name: The name of this test suite.
config.name = 'clang-taef-exec'

# Check that the object root is known.
te = getattr(config, 'te', None)
if te is None:
    # Otherwise, we haven't loaded the site specific configuration (the user is
    # probably trying to run on a test file directly, and either the site
    # configuration hasn't been created by the build system, or we are in an
    # out-of-tree build situation).

    # Check for 'clang_taef_exec_site_config' user parameter, and use that if available.
    site_cfg = lit_config.params.get('clang_taef_exec_site_config', None)
    if site_cfg and os.path.exists(site_cfg):
        lit_config.load_config(config, site_cfg)
        raise SystemExit
bin_dir = os.path.join(config.llvm_obj_root, config.llvm_build_mode, 'bin')
skip_taef_exec = lit_config.params.get('skip_taef_exec', None)
if skip_taef_exec or platform.system() != 'Windows':
  config.unsupported = True
else:
  dxil_dll = os.path.join(bin_dir, 'dxil.dll')
  if os.path.isfile(dxil_dll):
    print(str.format("\nUsed {} as validator\n", dxil_dll))
  else:
    print(str.format("\nCannot find validator in {}\n", bin_dir))
    dxexp = os.path.join(bin_dir, 'dxexp.exe')
    result = subprocess.run([dxexp], capture_output=True)
    no_experimental = "Experimental shader model feature failed"
    out = lit.util.convert_string(result.stdout)
    if no_experimental in out:
        print("""Developer mode is not enabled in Windows settings, preventing the experimental shader models feature.
Execution tests require DXIL.dll to sign shaders, or the experimental shader models feature to run unsigned shaders.""")
        config.unsupported = True

if config.unsupported == False:
  test_dll = os.path.join(bin_dir, 'ExecHLSLTests.dll')

  hlsl_data_dir = os.path.join(config.llvm_src_root, 'tools', 'clang', 'unittests', 'HLSLExec')

  test_dir = os.path.join(config.llvm_obj_root, config.llvm_build_mode, 'test')
  param_hlsl_data_dir = str.format('HlslDataDir={}', hlsl_data_dir)

  extra_params = ['/p:', 'ExperimentalShaders=*', '/p:', param_hlsl_data_dir]

  verbose = bool(getattr(config, 'verbose', False))
  if verbose != True:
      extra_params.append('/logOutput:')
      extra_params.append('LowWithConsoleBuffering')

  adapter = lit_config.params.get('adapter', None)
  if adapter:
      if adapter != '':
          extra_params.append('/p:')
          extra_params.append(str.format('Adapter={}', adapter))

  agility_sdk = lit_config.params.get('agility_sdk', None)
  if agility_sdk:
    extra_params.append('/p:')
    extra_params.append('D3D12SDKVersion=1')

  # use ';' to split multiple params.
  taef_extra_params = lit_config.params.get('taef_exec_extra_params', None)
  if taef_extra_params:
      extra_params.extend(taef_extra_params.split(';'))

  arch = getattr(config, 'taef_arch', None)
  arch_filter = str.format("@Architecture='{}'", arch)
  priority_filter = "@Priority<2"
  exec_future = lit_config.params.get('exec_future', None)
  if exec_future:
      priority_filter = "@Priority=2"

  select_filter = str.format("{} AND {}", priority_filter, arch_filter)

  config.test_format = lit.formats.TaefTest(config.te, test_dll, test_dir, select_filter, extra_params)
